//
//  XQPopView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

public class XQPopView: UIView,UIGestureRecognizerDelegate {

    private var popViewConfig : XQPopConfig
    
    private weak var contentView : UIView?
    typealias HiddenPop = () -> Void
    private var hiddenPop : HiddenPop?
    private var contentSize = CGSize.zero
    
    init(view: UIView,
         config : XQPopConfig,
         hidenHandle : HiddenPop? = nil) {
        
        popViewConfig = config
        
        super.init(frame: UIScreen.main.bounds)
        clipsToBounds = true
        
        contentView = view
        contentSize = view.bounds.size
        hiddenPop = hidenHandle
        
        backgroundColor = popViewConfig.bgColor
        
        configureGesture()
        
        addSubview(view)
        
        view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(contentSize)
        }
        view.transform = CGAffineTransform.identity.scaledBy(x: 0.01, y: 0.01)
        
        layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func backClick(){
        if popViewConfig.clickOutHidden {
          hiddenPop?()
        }
    }
    
    func showAnimate(){
        
        UIView.animate(withDuration: popViewConfig.animateDuration) {
            self.contentView?.transform = CGAffineTransform.identity.scaledBy(x: 1, y: 1)
        }
        
//        if popViewConfig.animateDamping {
//            UIView.animate(withDuration: popViewConfig.animateDuration, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: [], animations: {
//                self.layoutIfNeeded()
//            }) { (finished) in
//                
//            }
//        }else{
//            UIView.animate(withDuration: popViewConfig.animateDuration) {
//                self.layoutIfNeeded()
//            }
//        }
     }
    
    func hideAnimate(_ block : HiddenPop?){
        
        UIView.animate(withDuration: popViewConfig.animateDuration, animations: {
            self.contentView?.transform = CGAffineTransform.identity.scaledBy(x: 0.001, y: 0.001)
        }) { (finished) in
            block?()
        }
//        return
        
//        
//        UIView.animate(withDuration: popViewConfig.animateDuration, animations: {
//            self.layoutIfNeeded()
//        }) { (finished) in
//            block?()
//        }
    }
    
    private func configureGesture() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        tapGestureRecognizer.delegate = self
        self.addGestureRecognizer(tapGestureRecognizer)
    }
    
    @objc private func handleTap(_ sender: UITapGestureRecognizer) {
        let location = sender.location(in: self) // 获取点击位置
        if !self.contentView!.frame.contains(location) {
            hiddenPop?()
        }
    }
}
